#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
交易 - 微信公众号/小程序支付功能使用示例
"""

import os
import sys

# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))))

from examples.base_example import BaseExample
from jlpay.services.trans.mini.wx.official_pay_service import OfficialPayService
from jlpay.model.trans.mini.wx.official_pay import (
    OfficialPayRequest, OfficialPayResponse
)


class OfficialPayExample(BaseExample):
    """微信公众号/小程序支付示例类"""
    
    def __init__(self):
        """初始化示例类"""
        super().__init__("examples.trans.mini.wx.official_pay")
        self.service = OfficialPayService(self.config)
    
    def build_official_pay_request(self):
        """
        构建微信公众号/小程序支付请求
        
        Returns:
            OfficialPayRequest实例
        """
        request = OfficialPayRequest()
        request.set_mch_id("849584358120018")
        request.set_term_no("60131234")
        request.set_pay_type("wxpay")
        request.set_open_id("oUpF8uMuAJO_M2pxb1Q9zNjWeS6o")
        request.set_sub_appid("wxd930ea53421a258f4f")
        request.set_limit_pay("no_credit")
        request.set_out_trade_no("ld201911023465529")
        request.set_payment_valid_time("10")
        request.set_body("测试微信公众号支付商品")
        request.set_attach("测试微信公众号支付描述")
        request.set_total_fee("150")
        request.set_remark("测试微信公众号支付备注")
        request.set_notify_url("https://help.jlpay.com/api/docs/home")
        request.set_op_user_id("test_user")
        request.set_op_shop_id("GD_SZ_001")
        request.set_device_info("N50007A8890")
        request.set_mch_create_ip("127.0.0.1")
        request.set_longitude("116.397128")
        request.set_latitude("39.916527")
        request.set_goods_tag("test_tag")
        return request
    
    def demo(self):
        """执行微信公众号/小程序支付示例"""
        self.logger.info("开始执行微信公众号/小程序支付示例")
        
        # 构建请求
        request = self.build_official_pay_request()
        
        # 发送请求并处理响应
        self.send_request_and_handle_response(
            self.service.official_pay,
            "微信公众号/小程序支付",
            request
        )
        
        self.logger.info("微信公众号/小程序支付示例执行完毕")


if __name__ == "__main__":
    logger = OfficialPayExample().logger
    logger.info("==================================================")
    logger.info("===== 微信公众号/小程序支付示例 开始 =====")
    logger.info("==================================================")
    
    example = OfficialPayExample()
    example.demo()
    
    logger.info("==================================================")
    logger.info("===== 微信公众号/小程序支付示例执行完毕 =====")
    logger.info("==================================================") 